
const app = getApp()
Page({
  data: {
    title:'威化市东山商贸有限公司',
    yearArr: ['2017'],
    index:0,
    curNav:6,
    pdfList: [{
      name: '凭证',
      img: '../image/voucher.png',
      page: '../voucher/voucher'
    }, {
      name: '利润表',
      img: '../image/incomeStatement.png',
      page: '../incomeStatement/incomeStatement'
    },{ 
      name: '现金流量表',
      img: '../image/cashFlowStatement.png', 
      page:'../cashFlowStatement/cashFlowStatement' 
    },{ 
      name: '资产负债表', 
      img: '../image/balanceSheet.png', 
      page: '../balanceSheet/balanceSheet'
    }, {
      name: '余额表',
      img: '../image/balance.png',
      page: '../balance/balance'
    },{ 
      name: '明细账',
      img: '../image/subsidiaryLedger.png', 
      page: '../subsidiaryLedger/subsidiaryLedger'
    },{
      name: '纳税统计表',
      img: '../image/taxEntry.png',
      page: '../taxEntry/taxEntry'
    }, {
      name: '报税回执',
      img: '../image/taxReceipt.png',
      page: '../taxReceipt/taxReceipt'
    }],
    
    navLeftItems: [  
      { id: 0, text: '1月', month: '01', disable: false },
      { id: 1, text: '2月', month: '02', disable: false},
      { id: 2, text: '3月', month: '03', disable: false},
      { id: 3, text: '4月', month: '04', disable: false},
      { id: 4, text: '5月', month: '05', disable: false},
      { id: 5, text: '6月', month: '06', disable: false},
      { id: 6, text: '7月', month: '07', disable: false},
      { id: 7, text: '8月', month: '08', disable: false},
      { id: 8, text: '9月', month: '09', disable: false},
      { id: 9, text: '10月', month: '10', disable: false},
      { id: 10, text: '11月', month: '11', disable: false},
      { id: 11, text: '12月', month: '12', disable: false},
    ],
    closePeriod: '',
    startPeriod: '',
    marginTop: ''
  },
  onLoad(){   
    wx.showLoading({
      title: '加载中',
    })

    this.setMarginTop()

    this.getVersion()
    
  },
  handlePeriod(start,close){
    this.data.yearArr = [];
    var lastSelectMonth = close.substring(4, 6)
    var start = Number(start.substring(0,4));
    var close = Number(close.substring(0, 4));
    
    this.data.navLeftItems.forEach((item,index)=>{
      if (item.month == lastSelectMonth){
        this.setData({
          curNav: item.id
        })
      }
    })

    for (let i = start; i <= close; i++){
      this.data.yearArr.push(i.toString());
    }
    
    this.data.index = this.data.yearArr.length - 1;

    let year = this.data.yearArr[this.data.index];
    this.setDisableMonth(year)

    this.setData({
      yearArr: this.data.yearArr,
      index: this.data.index
    })

    wx.hideLoading();
  },
  // 点击预览
  view(e){
    let date = this.data.yearArr[this.data.index] + this.data.navLeftItems[this.data.curNav].month;
    wx.navigateTo({
      url: e.currentTarget.dataset.page + '?data=' + date
    })
  },
  //switchTab事件处理函数
  switchRightTab: function (e) {
    if(e.currentTarget.dataset.item.disable==true) return
    let id = e.currentTarget.dataset.item.id;
    this.setData({
      curNav: id
    })
  },
  //Picker
  bindPickerChange: function (e) {
    let year = this.data.yearArr[e.detail.value];
    this.setDisableMonth(year)
    this.setData({
      index: e.detail.value
    })
  },

  setDisableMonth(year){
    this.data.navLeftItems.forEach((item) => {
      let period = year + item.month;
      if (Number(period) < Number(this.data.startPeriod) ||
        Number(period) > Number(this.data.closePeriod)) {
        item.disable = true
      }
    })
    this.setData({
      navLeftItems: this.data.navLeftItems
    })
  },

  setMarginTop(){
    let marginTop = app.globalData.windowHeight - 570;

    if (marginTop < 0) {
      marginTop = 15;
    } else if (marginTop < 60 && 0 < marginTop) {
      marginTop = 30;
    } else {
      marginTop = 45;
    }

    this.setData({
      marginTop: marginTop / 3
    })
  },

  getVersion(){
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/accounts/login',
      method: 'POST',
      data: {
        loginId: 'wly',
        hashPassword: 'admin'
      },
      header: {
        version: app.globalData.version
      },
      success: function (res) {
        app.globalData.version = res.header.version;
        if (res.data.code == 667 || res.data.code == 666) {
          wx.request({
            url: app.globalData.url + 'api/accounts/login',
            method: 'POST',
            data: {
              loginId: 'wly',
              hashPassword: 'admin'
            },
            header: {
              version: res.header.version
            },
            success: function (res1) {
              app.globalData.no = res1.data.no;
              app.globalData.token = res1.data.token;
              wx.request({
                url: app.globalData.url + 'api/accountbooks/listall?page=0&&size=1',
                method: 'POST',
                header: {
                  token: app.globalData.token,
                  version: app.globalData.version
                },
                data: {
                  accounterNo: app.globalData.no
                },
                success: function (res2) {
                  app.globalData.accountBookId = res2.data.content[0].id;
                  app.globalData.company = res2.data.content[0].bookName;

                  that.setData({
                    title: res2.data.content[0].bookName
                  })

                  wx.request({
                    url: app.globalData.url + 'api/accountbooks/' + app.globalData.accountBookId,
                    method: 'GET',
                    header: {
                      token: app.globalData.token,
                      version: app.globalData.version
                    },
                    success: function (response) {
                      that.data.startPeriod = response.data.startPeriod;
                      that.data.closePeriod = response.data.closePeriod;
                      that.handlePeriod(response.data.startPeriod, response.data.closePeriod)
                    }
                  })
                }
              })
            }
          })
        }
      },
      fail:function(res){
        wx.hideLoading();
        wx.showToast({
          title: '加载失败，请重试！',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
})
